#include "MonitoringRealTabModel.h"

MonitoringRealTabModel::MonitoringRealTabModel(QObject *)
{

}

int MonitoringRealTabModel::rowCount(const QModelIndex &) const
{
    return dataList.size();
}

int MonitoringRealTabModel::columnCount(const QModelIndex &) const
{
    return 7;
}

QVariant MonitoringRealTabModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (role == Qt::DisplayRole) {
        const auto &data = dataList[index.row()];
        switch (index.column()) {
        case 0: return data.user_id;
        case 1: return data.device_id;
        case 2: return data.timestamp;
        case 3: return data.temperature;
        case 4: return data.humidity;
        case 5: return data.light;
        case 6: return data.co2;
        case 7: return data.pressure;
        default: return QVariant();
        }
    }else if (role == Qt::TextAlignmentRole) {
        return Qt::AlignCenter;
    }
    return QVariant();
}

QVariant MonitoringRealTabModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
        switch (section) {
        case 0: return "用户id";
        case 1: return "设备id";
        case 2: return "时间";
        case 3: return "温度";
        case 4: return "湿度";
        case 5: return "光照";
        case 6: return "二氧化碳浓度";
        case 7: return "气压";
        default: return QVariant();
        }
    }
    return QVariant();
}

void MonitoringRealTabModel::updateAllDeviceList(const QList<MonitoringDataModel> &newList)
{
    beginResetModel();
    dataList = newList;
    endResetModel();
}
